package org.dromara.netbar.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.netbar.domain.bo.NetbarGiftPackItemBo;
import org.dromara.netbar.domain.vo.NetbarGiftPackItemVo;
import org.dromara.netbar.domain.NetbarGiftPackItem;
import org.dromara.netbar.mapper.NetbarGiftPackItemMapper;
import org.dromara.netbar.service.INetbarGiftPackItemService;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 礼包-卡券关联Service业务层处理
 *
 * @author Lion Li
 * @date 2025-07-10
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class NetbarGiftPackItemServiceImpl implements INetbarGiftPackItemService {

    private final NetbarGiftPackItemMapper baseMapper;
    /**
     * 查询礼包-卡券关联
     *
     * @param packItemId 主键
     * @return 礼包-卡券关联
     */
    @Override
    public NetbarGiftPackItemVo queryById(Long packItemId){
        return baseMapper.selectVoById(packItemId);
    }

    /**
     * 分页查询礼包-卡券关联列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 礼包-卡券关联分页列表
     */
    @Override
    public TableDataInfo<NetbarGiftPackItemVo> queryPageList(NetbarGiftPackItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NetbarGiftPackItem> lqw = buildQueryWrapper(bo);
        Page<NetbarGiftPackItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的礼包-卡券关联列表
     *
     * @param bo 查询条件
     * @return 礼包-卡券关联列表
     */
    @Override
    public List<NetbarGiftPackItemVo> queryList(NetbarGiftPackItemBo bo) {
        LambdaQueryWrapper<NetbarGiftPackItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NetbarGiftPackItem> buildQueryWrapper(NetbarGiftPackItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NetbarGiftPackItem> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(NetbarGiftPackItem::getPackItemId);
        lqw.eq(bo.getPackId() != null, NetbarGiftPackItem::getPackId, bo.getPackId());
        lqw.eq(bo.getCouponId() != null, NetbarGiftPackItem::getCouponId, bo.getCouponId());
        lqw.eq(bo.getCouponCount() != null, NetbarGiftPackItem::getCouponCount, bo.getCouponCount());
        return lqw;
    }

    /**
     * 新增礼包-卡券关联
     *
     * @param bo 礼包-卡券关联
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(NetbarGiftPackItemBo bo) {
        NetbarGiftPackItem add = MapstructUtils.convert(bo, NetbarGiftPackItem.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPackItemId(add.getPackItemId());
        }
        return flag;
    }

    /**
     * 修改礼包-卡券关联
     *
     * @param bo 礼包-卡券关联
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(NetbarGiftPackItemBo bo) {
        NetbarGiftPackItem update = MapstructUtils.convert(bo, NetbarGiftPackItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NetbarGiftPackItem entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除礼包-卡券关联信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public Boolean deleteByPackId(Long packId) {
        return baseMapper.deleteByPackId(packId)>0;
    }

    @Override
    public List<NetbarGiftPackItemVo> queryListByPackIds(List<Long> packIds) {
        if (CollectionUtil.isEmpty(packIds)) {
            return Collections.emptyList();
        }
        return baseMapper.queryListByPackIds(packIds);
    }

    @Override
    public Map<Long, Integer> countMapByCouponIds(Long[] couponIds) {
        if (null == couponIds || 0 == couponIds.length) {
            return Collections.emptyMap();
        }
        return baseMapper.countMapByCouponIds(couponIds);
    }
}
